package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MachineAbnormalConstant;
import com.tbit.main.pojo.GeoreGion;
import com.tbit.main.service.GeoreGionService;
import com.tbit.main.service.GeoreMachineService;
import com.tbit.main.service.MachineBorrowService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 分区车辆
 *
 * @author LMY
 * @create 2021-06-30 20:23
 */
@RestController
@RequestMapping("/georeMachine")
public class GeoreMachineController {

    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private GeoreMachineService georeMachineService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineBorrowService machineBorrowService;

    /**
     * 查询
     *
     * @param token
     * @param accountId
     * @param regionId
     * @return
     */
    @RequestMapping("/stream")
    public JsonResult getStream(String token, Integer accountId,
                                @RequestParam(value = "regionId", required = true) List<Integer> regionId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<Map<String, Object>> regionList = new LinkedList<>();

        /**分区名称*/
        List<GeoreGion> georeGions = georeGionService.getByRegionIds(regionId);
        Map<Integer, String> geore = new HashMap<>();
        for (GeoreGion georeGion : georeGions) {
            geore.put(georeGion.getRegionId(), georeGion.getRegionName());
        }

        /**车辆总数*/
        List<Map<String, Object>> countList = georeMachineService.getMachine(regionId);
        Map<Integer, Integer> countMap = new HashMap<>();
        for (Map<String, Object> mapCount : countList) {
            countMap.put(Integer.parseInt(mapCount.get("regionId").toString()), Integer.parseInt(mapCount.get("machineCount").toString()));
        }

        /**正在骑行车辆数*/
        List<Map<String, Object>> machineUseIng = machineBorrowService.getGroupByRegionId(regionId);
        Map<Integer, Integer> useMap = new HashMap<>();
        for (Map<String, Object> mapCount : machineUseIng) {
            useMap.put(Integer.parseInt(mapCount.get("regionId").toString()), Integer.parseInt(mapCount.get("useMachine").toString()));
        }


        /**流入车辆*/
        List<Map<String, Object>> intoList = georeMachineService.intoMachine(regionId);
        Map<Integer, Integer> intoMap = new HashMap<>();
        for (Map<String, Object> mapInto : intoList) {
            intoMap.put(Integer.parseInt(mapInto.get("regionId").toString()), Integer.parseInt(mapInto.get("machineCount").toString()));
        }

        /**流出车辆*/
        List<Map<String, Object>> outList = georeMachineService.outMachine(regionId);
        Map<Integer, Integer> outMap = new HashMap<>();
        for (Map<String, Object> mapOut : outList) {
            outMap.put(Integer.parseInt(mapOut.get("beforeRegionId").toString()), Integer.parseInt(mapOut.get("machineCount").toString()));
        }

        /**故障车辆*/
        List<Map<String, Object>> faultList = georeMachineService.getFault(regionId);
        Map<Integer, Integer> faultMap = new HashMap<>();
        for (Map<String, Object> mapFault : faultList) {
            faultMap.put(Integer.parseInt(mapFault.get("regionId").toString()), Integer.parseInt(mapFault.get("machineCount").toString()));
        }

        /**闲置车辆*/
        List<Map<String, Object>> borrowList = georeMachineService.getBorrow(regionId);
        Map<Integer, Integer> borrowMap = new HashMap<>();
        for (Map<String, Object> mapBorrow : borrowList) {
            borrowMap.put(Integer.parseInt(mapBorrow.get("regionId").toString()), Integer.parseInt(mapBorrow.get("machineCount").toString()));
        }

        /**缺电车辆*/
        List<Map<String, Object>> powerList = georeMachineService.getPower(regionId, accountId);
        Map<Integer, Integer> powerMap = new HashMap<>();
        for (Map<String, Object> mapPower : powerList) {
            powerMap.put(Integer.parseInt(mapPower.get("regionId").toString()), Integer.parseInt(mapPower.get("machineCount").toString()));
        }

        /**异常车辆*/
        List<Map<String, Object>> abnormalList = georeMachineService.getAbnormal(regionId);
        Map<String, Integer> abnormalMap = new HashMap<>();
        for (Map<String, Object> mapAbnormal : abnormalList) {
            abnormalMap.put(mapAbnormal.get("regionId") + "_" + mapAbnormal.get("abnormalType"), Integer.parseInt(mapAbnormal.get("machineCount").toString()));
        }

        for (Integer region : regionId) {
            Map<String, Object> mapMachine = new HashMap<>();
            mapMachine.put("region", region);
            if (geore.containsKey(region)) {
                mapMachine.put("regionName", geore.get(region));
            }
            /**车辆总数*/
            if (countMap.containsKey(region)) {
                mapMachine.put("count", countMap.get(region));
            } else {
                mapMachine.put("count", 0);
            }
            /**正在骑行*/
            if (useMap.containsKey(region)) {
                mapMachine.put("riding", useMap.get(region));
            } else {
                mapMachine.put("riding", 0);
            }
            /**流入*/
            if (intoMap.containsKey(region)) {
                mapMachine.put("into", intoMap.get(region));
            } else {
                mapMachine.put("into", 0);
            }
            /**流出*/
            if (outMap.containsKey(region)) {
                mapMachine.put("out", outMap.get(region));
            } else {
                mapMachine.put("out", 0);
            }
            /**故障*/
            if (faultMap.containsKey(region)) {
                mapMachine.put("fault", faultMap.get(region));
            } else {
                mapMachine.put("fault", 0);
            }
            /**闲置*/
            if (borrowMap.containsKey(region)) {
                mapMachine.put("borrow", borrowMap.get(region));
            } else {
                mapMachine.put("borrow", 0);
            }
            /**缺电*/
            if (powerMap.containsKey(region)) {
                mapMachine.put("power", powerMap.get(region));
            } else {
                mapMachine.put("power", 0);
            }
            /**超区*/
            if (abnormalMap.containsKey(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_CCQY)) {
                mapMachine.put("CCQY", abnormalMap.get(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_CCQY));
            } else {
                mapMachine.put("CCQY", 0);
            }
            /**站外*/
            if (abnormalMap.containsKey(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_ZW)) {
                mapMachine.put("ZW", abnormalMap.get(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_ZW));
            } else {
                mapMachine.put("ZW", 0);
            }
            /**无信号*/
            if (abnormalMap.containsKey(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_WXH)) {
                mapMachine.put("WXH", abnormalMap.get(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_WXH));
            } else {
                mapMachine.put("WXH", 0);
            }
            /**电池异常*/
            if (abnormalMap.containsKey(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_DCYC)) {
                mapMachine.put("DCYC", abnormalMap.get(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_DCYC));
            } else {
                mapMachine.put("DCYC", 0);
            }
            /**禁停区异常*/
            if (abnormalMap.containsKey(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_JTYC)) {
                mapMachine.put("JTQY", abnormalMap.get(region + "_" + MachineAbnormalConstant.MACHINE_ABNORMAL_JTYC));
            } else {
                mapMachine.put("JTQY", 0);
            }
            regionList.add(mapMachine);
        }

        return JsonResult.succ(regionList);
    }
}
